﻿using System;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class ctl_seat_map : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public int RetreiveSeatMap(char type, string airline, string flt_no, string plan_id, DateTime flt_date)
        {
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            SqlParameter para = cmd.Parameters.AddWithValue("@airline", airline);
            para = cmd.Parameters.AddWithValue("@flt_no", flt_no);
            if (type == 'P')  //Plan
            {
                cmd.CommandText = "bll_plan_get_seat_map";
                para = cmd.Parameters.AddWithValue("@plan_id", plan_id);
            }
            else if (type == 'F')//Flight
            {
                cmd.CommandText = "bll_flt_get_seat_map";
                para = cmd.Parameters.AddWithValue("@flt_date", flt_date);
                para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            }
            else
                return 3;

            DataTable dtMap = new DataTable();
            try
            {
                ret_code = TinyDAL.Exec(ref cmd, ref dtMap);
                if (type == 'F')
                {
                    switch (ret_code)
                    {
                        case 0:
                            //lblError.Text = "OK.";
                            break;
                        case 1:
                            //lblError.Text = "Ticket not found, or all tickets in the order are checked-in!";
                            return ret_code;
                        case 2:
                            //lblError.Text = "Seat map not found!";
                            return ret_code;
                        case 50:
                            //lblError.Text = "Unauthorized airline!";
                            return ret_code;
                        default:
                            //lblError.Text = "Error:" + ret_code.ToString();
                            return ret_code;
                    }
                }

                tabSeatMap.Rows.Clear();
                TableRow tempRow = new TableRow();
                TableCell tempCell;
                Int16 last_col = -1;
                string space_type, seat_status/*, inf_seat*/;
                int curr_x = 1;
                foreach (DataRow row in dtMap.Rows)
                {
                    if (last_col != Convert.ToInt16(row["y"])/* || last_col == max_y + 1*/)
                    {
                        tabSeatMap.Rows.Add(tempRow);
                        if (last_col - 1 != Convert.ToInt16(row["y"]))
                        {
                            for (last_col--; last_col > Convert.ToInt16(row["y"]); last_col--)
                            {
                                tempRow = new TableRow();
                                tempCell = new TableCell();
                                tempCell.HorizontalAlign = HorizontalAlign.Left;
                                tempCell.Text = "<b>" + last_col.ToString() + "</b>";
                                tempRow.Cells.Add(tempCell);
                                tabSeatMap.Rows.Add(tempRow);
                            }
                        }

                        tempRow = new TableRow();
                        last_col = Convert.ToInt16(row["y"]);
                        tempCell = new TableCell();
                        tempCell.Text = "<b>" + last_col.ToString() + "</b>";
                        tempCell.HorizontalAlign = HorizontalAlign.Left;
                        tempRow.Cells.Add(tempCell);

                        curr_x = 1;
                    }
                    if (curr_x != Convert.ToInt16(row["x"]))
                    {
                        for (; curr_x < Convert.ToInt16(row["x"]); curr_x++)
                        {
                            tempCell = new TableCell();
                            tempCell.Text = "&nbsp;";
                            tempRow.Cells.Add(tempCell);
                        }
                    }
                    //if (last_col != Convert.ToInt16(row["y"]) && last_col != -1)
                    //{
                    //    tabSeatMap.Rows.Add(tempRow);
                    //    tempRow = new TableRow();
                    //    last_col = Convert.ToInt16(row["y"]);
                    //}
                    //if (last_col == -1)
                    //{
                    //    last_col = Convert.ToInt16(row["y"]);
                    //}

                    tempCell = new TableCell();
                    space_type = row["cell_type"].ToString();
                    string buf1, buf2;
                    if (space_type == "S")//seat
                    {
                        if (type == 'F')
                            seat_status = row["status"].ToString();
                        else
                            seat_status = "";

                        buf2 = "type='radio' value='" + row["x"].ToString() + "|" + row["y"].ToString() + "'>" + row["row"].ToString() + row["col"].ToString()
                            + "|" + row["cabin"].ToString() + "|" + row["class"].ToString().TrimEnd() + "|" + row["ori_airport"].ToString().TrimEnd() + "-" + row["des_airport"].ToString().TrimEnd() 
                            + "|" + row["seat_type"].ToString().TrimEnd() + "|" + row["attributes"].ToString().TrimEnd();
                        if (type == 'F')
                        {
                            switch (seat_status)
                            {
                                case "A"://available
                                    buf1 = "<input name='CELL' ";//style='background-color:lightgreen' ";
                                    tempCell.BackColor = System.Drawing.Color.LightGreen;
                                    break;
                                case "O"://occupied
                                    buf1 = "<input name='CELL' style='background-color:Cyan' ";  //可重复选自己已选的座位，以便取消预选座位
                                    tempCell.BackColor = System.Drawing.Color.Cyan;
                                    break;
                                default://impossible!
                                    buf1 = "<input name='CELL' style='background-color:gray'";
                                    tempCell.BackColor = System.Drawing.Color.Gray;
                                    break;
                            }
                        }
                        else  //Plan
                        {
                            buf1 = "<input name='CELL' ";
                        }
                        tempCell.Text = "&nbsp;"+buf1 + buf2;

                    }
                    else if ("R" == row["cell_type"].ToString())//"R"Road
                    {
                        tempCell.Text = "&nbsp;<input name='CELL' type='radio' value='" + row["x"].ToString() + "|" + row["y"].ToString() + "'>=</input>";
                        //str = str.Append("="/*row["cabin"].ToString()*/);
                    }
                    else
                    {
                        tempCell.Text = "&nbsp;<input name='CELL' type='radio' value='" + row["x"].ToString() + "|" + row["y"].ToString() + "'>?</input>";
                        //str = str.Append("?"/*row["cabin"].ToString()*/);
                    }

                    tempRow.Cells.Add(tempCell);
                    curr_x = Convert.ToInt16(row["x"]) + 1;
                }
                dtMap.Clear();
                tabSeatMap.Rows.Add(tempRow);

                tempRow = new TableRow();
                tempCell = new TableCell();
                tempCell.Text = "<b>Y/X</b>";
                tempRow.Cells.Add(tempCell);
                for (ret_code = 1; ret_code <= curr_x - 1; ret_code++)
                {
                    tempCell = new TableCell();
                    tempCell.Text = "&nbsp;<b>" + ret_code.ToString() + "</b>";
                    //tempCell.HorizontalAlign = HorizontalAlign.Center;
                    //tempCell.Width = System.Web.UI.WebControls.Unit.Pixel(200);
                    tempRow.Cells.Add(tempCell);
                }
                tabSeatMap.Rows.Add(tempRow);

                return 0;
            }
            catch (Exception ex)
            {
                return 100;
                //if (Application["DEBUG"].ToString() == "1")
                //lblError.Text = ex.Message;
                //else
                //lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}